package desgin.pattern.structure.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 
 * @Type BusinessDymanicProxy
 * @Desc 动态代理类
 * @author shuai.zuo
 * @date 2015年3月9日
 * @Version V1.0
 */
public class BusinessDymanicProxy implements InvocationHandler {
    private Object obj;

    public BusinessDymanicProxy(Object obj) {
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("do sth before dynamic real runs");
        Object result = method.invoke(obj, args);
        System.out.println("do sth after dynamic real runs");
        return result;
    }

    public static Object createProxy(Object obj) {
        Class<?> cls = obj.getClass();
        return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), new BusinessDymanicProxy(obj));
    }

}
